﻿// Namespace LyncServerExtension: Common functions.

// Namespace creation, and protection against global environment modification.
var LyncServerExtension = (function (ns) {

    // Adding the "map" function to the "Array" prototype if not present.
    if (!Array.prototype.map)
        Array.prototype.map = function (f) {
            var res = new Array(this.length);

            for (var i = 0; i < res.length; ++i)
                res[i] = f(this[i]);

            return res;
        }

    // Adding the "each" function to the "Array" prototype if not present.
    if (!Array.prototype.each)
        Array.prototype.each = function (f) {
            for (var i = 0; i < this.length; ++i)
                f(this[i]);
        }

    // Adding the "each" function to the "Array" prototype if not present.
    if (!Array.prototype.find)
        Array.prototype.find = function (pred) {
            for (var i = 0; i < this.length; ++i)
                if (pred(this[i]))
                    return this[i];
            return false;
        }

    // Adding the "filter" function to the "Array" prototype.
    Array.prototype.filter = function (pred) {
        var acc = []

        for (var i = 0; i < this.length; ++i)
            if (pred(this[i]))
                acc.push(this[i]);
        return acc;
    }

    // Adding the "bind" function to the "Function" prototype if not present.
    if (!Function.prototype.bind)
        Function.prototype.bind = function (obj) {
            var _self = this;

            return function () {
                _self.apply(obj, arguments);
            }
        }

    // Adding the "startsWith" function to the "String" prototype.
    String.prototype.startsWith = function (s) {
        if (s.length > this.length)
            return false;

        return (this.substring(0, s.length) == s);
    }

    return ns;
})({});